import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'controller/location_controller.dart';

class HomeLocationWidget extends StatefulWidget {
  final String pageTag;
  final bool isAdCode;

  const HomeLocationWidget(
      {Key key, @required this.pageTag, this.isAdCode = false})
      : super(key: key);

  @override
  _HomeLocationWidgetState createState() => new _HomeLocationWidgetState();
}

class _HomeLocationWidgetState extends State<HomeLocationWidget> {
  LocationGetController _locationGetController;

  @override
  void initState() {
    super.initState();
    _locationGetController = Get.put(
        LocationGetController(isAdCode: widget.isAdCode),
        tag: widget.pageTag);
    Future.delayed(Duration.zero, () {
      _locationGetController.initFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      tag: widget.pageTag,
      id: "locationRoot",
      builder: (LocationGetController controller) {
        bool noLocationPermisson = controller.noLocationPermisson;
        if (!noLocationPermisson) {
          return IconButton(
              onPressed: () {
                _locationGetController.initFunction();
              },
              icon: const Icon(Icons.location_off));
        }
        return InkWell(
          onTap: () {
            _locationGetController.showLocationFunction(context);
          },
          child: Container(
            padding: EdgeInsets.all(12),
            color: Colors.white,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Image.asset(
                      "assets/images/3.0x/location_icon.png",
                      width: 28,
                      height: 28,
                    ),
                    SizedBox(
                      width: 12,
                    ),
                    Expanded(
                      child: buildCurrentLocation(controller),
                    ),
                  ],
                ),
                buildWeatherWidget(controller),
              ],
            ),
          ),
        );
      },
    );
  }

  buildCurrentLocation(LocationGetController controller) {
    return Text(controller.getCurrentAddress());
  }

  buildWeatherWidget(LocationGetController controller) {
    if (!widget.isAdCode) {
      return SizedBox();
    }
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Image.asset(
          "assets/images/3.0x/weather_icon.png",
          width: 28,
          height: 28,
        ),
        SizedBox(
          width: 12,
        ),
        Expanded(
          child: Text(controller.getCurrentWeather()),
        ),
      ],
    );
  }
}
